var win=$(window);
var imgCollapse=_TMPL_+"pAdmin/img/collapsed.gif";
var imgExpand=_TMPL_+"pAdmin/img/expanded.gif";
var imgLoading=_TMPL_+"pAdmin/img/loading.gif";
var lastVisitedMenu=null;
var searchForm={};
var yaModal=null;

function adjustPage(){
    $('yabody').setStyle('height',(win.getSize().y-50)+'px');
}

/**
 * 历史记录管理，只管back() @todo 同一个地址不重复连续添加多次
 */
var histories=[];
function addURLHistory(href){
    histories.push(href);
    if(histories.length>3){
        histories=histories.slice(3);
    }
}
function pageBack(){
    if(histories.length>1){
        histories.pop();
        $('mainFrame').setProperty('src',histories.pop());
    }else{
        $('mainFrame').contentWindow.history.back();
    }
}
/**
 * 显示消息
 *
 * options:
 * type:消息类型:success|failure|running，默认success，running一般用于ajax请求场合
 * disappear:integer 于多少秒后自动消失
 * clickclose:false|true,点击后关闭
 */
function showMsg(message,options){
    $("__fixedmsg__").set('opacity','0');
    options = options || {};
    var maskDiv = $("__msg_mask__"),msg = $("__msg__");
    maskDiv.setStyle('display','block');
    msg.set('html',message);
    msg.setStyle('display','block');
    var size = msg.getSize(),winSize = win.getSize();
    msg.setStyles({
        'top' : (winSize.y-size.y)/3+'px',
        'left' : (winSize.x-size.x)/2+'px'
    });
    var dis = function(){
        msg.fade('out');
        maskDiv.fade('out');
        if(options.onDisappear){
            options.onDisappear.run();
        }
    };
    if( options.disappear ){
        dis.delay( options.disappear*1000 );
    }
    msg.onclick=maskDiv.onclick=function(){
        if(options.clickclose){
            dis.run();
        }
    }
    msg.fade('show');
    maskDiv.set('opacity','0.8');
}

function fixedMsg(type,msg){
    var msgSpan=$("__fixedmsg__");
    msgSpan.set('html',msg+'<a class="fixedMsgA" href="javascript:hideFixedMsg()">关闭</a>');
    var size=msgSpan.getSize();
    var winSize=win.getSize();
    msgSpan.setStyle('left',(winSize.x-size.x)/2+'px');
    msgSpan.removeClass().addClass((type)?'success':'failure');
    msgSpan.set('opacity','0.9');
}
function hideFixedMsg(){
    $("__fixedmsg__").fade('out');
}

var titleSuffix = null;
function title(t){
    document.title = (t.trim().length==0) ? titleSuffix : t + ' ‹  ' + titleSuffix;
}

win.addEvent('domready',function(){
    titleSuffix = document.title;
    adjustPage();
    win.addEvent('resize',function(){ adjustPage(); });
    //add the effects to the nav menu
    $$('#menuWrapper img').addEvent('click',function(){
            var img=this,ul=img.getParents('ul');
            var next=ul.getNext();
            if(next.getStyle('display')=='none'){
                next.setStyle('display','block');
                img.setProperty('src',imgExpand);
            }else{
                next.setStyle('display','none');
                img.setProperty('src',imgCollapse);
            }
            var next2=next.getNext();
            if(next2!=null){
                next2.toggleClass('menu1top');
            }
        });
    var links=$$('#menuWrapper ul.menu2 a');
    links.setProperty('target','mainFrame');
    links.addEvents({
            'mouseover':function(){
                if( ! this.hasClass('current'))   this.addClass('hover');
            },
            'mouseout':function(){
                this.removeClass('hover');
            },
            'click':function(){
                this.blur();
                if(lastVisitedMenu){
                    if(lastVisitedMenu==this) return;
                    lastVisitedMenu.removeClass('current');
                }
                this.removeClass('hover').addClass('current');
                lastVisitedMenu=this;
            }
        });
    if(typeof _goLink !='undefined'){
        Cookie.dispose('goLink');
        $('mainFrame').setProperty('src',_goLink);
    }
    yaModal=new yaModal($(document.body));
    $("searchAdvanced").addEvent('click',function(){
        yaModal.html({
            'src':'http://localhost:9999/RoyalCMS/index.php/pUser/search',
            'title':'查找用户'
        });
    });
    initSearch();
});

/**
 * 搜索
 */
var si,sf,sb,sa;
function initSearch(){
    si=$('searchInput');
    sf=$('searchForm');
    sb=$('searchBtn');
    sa=$('searchAdvanced');
    si.addEvents({
        'blur':function(){
            this.setProperty('class','textInput');
        },
        'focus':function(){
            this.setProperty('class','textInputFocus');
        }
    });
    sb.addEvents({
        'click':function(){
            if(si.getProperty('value').trim()==''){
                showMsg('请先输入关键字',{
                    clickclose:true,
                    onDisappear:function(){
                        si.focus();
                    }
                });
                return;
            }
            if(sf.getProperty('method').toLowerCase()=='post'){
                sf.submit();
            }else{
                $('mainFrame').setProperty('src',sf.getProperty('action')+'&'+sf.toQueryString());
            }
        }
    });
    sa.addEvents({
        'click':function(){
            yaModal.html(sa.retrieve('url'));
        }
    });
}
function registerSearch(opts){
    si.setProperty('name',opts.name || 'q'); //set the property 'name' of the input HTMLElement
    sf.setProperty('action',opts.action);
    sf.setProperty('method',opts.method || 'get');
    if(opts.advanced){
        sa.store('url',opts.advanced);
        sa.setProperty('disabled',false);
    }else{
        sa.setProperty('disabled',true);
    }
    sb.setProperty('html','查找'+opts.tag);
}
